package com.jdkreader.concurrency.concurrent.pipeline;

import java.util.HashSet;
import java.util.Set;

public class RobotPool {

    private Set<Robot> pool=new HashSet<Robot>();

    public synchronized void add(Robot r){
        pool.add(r);
        notifyAll();
    }

    public synchronized void release(Robot r){
         add(r);
    }

    public synchronized void hire(Class<? extends Robot> robotType,Assembler d) throws InterruptedException{
        for(Robot r:pool){
            if (r.getClass().equals(robotType)) {
                pool.remove(r);
                r.assignAssembler(d);
                r.engage(); //开启机器
                return;
            }
        }
        //没有找到可用的机器，等待有可用机器出现
        wait();
        hire(robotType,d);
    }
}
